using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;


/// <summary>
/// Summary description for the progress-monitor server API 
/// </summary>

namespace TN5Y.Server
{
    public interface IProgressMonitor
    {
        string StartProcess(string processName, string processStatusMessage, Int16 percentComplete);
        string SetProcessStatus(string GUID, string processStatusMessage, Int16 percentComplete);
        string GetProcessStatus(string GUID);
        void EndProcess(string ProcessGUID);
    }


    //}
    public class ProgressMonitor : IProgressMonitor
    {
        // Sets the current status of the task and returns the GUID
        public string StartProcess(string processName, string processStatusMessage, Int16 percentComplete)
        {
            string sql;
            string GUID = this.GetNewGUID();
            sql = "insert into ProcessStatus values (@GUID,@ProcessName,@ProcessStatusMessage, getdate(),null,@PercentComplete)";
            SqlHelper.ExecuteNonQuery(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
                    new SqlParameter("GUID", GUID),
                    new SqlParameter("ProcessName", processName),
                    new SqlParameter("ProcessStatusMessage", processStatusMessage),
                    new SqlParameter("PercentComplete", percentComplete));
            return GUID;
        }
        // Sets the current status of the task and returns the GUID
        public string SetProcessStatus(string GUID, string processStatusMessage, Int16 percentComplete)
        {
            string sql;
            sql = "update ProcessStatus set ProcessStatusMessage = @ProcessStatusMessage, " +
                " PercentComplete = @PercentComplete where GUID = @GUID ";

            SqlHelper.ExecuteNonQuery(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
                    new SqlParameter("GUID", GUID),
                    new SqlParameter("ProcessStatusMessage", processStatusMessage),
                    new SqlParameter("PercentComplete", percentComplete));
            return GUID;
        }

        private string GetNewGUID()
        {
            string sql = "declare @NewID UNIQUEIDENTIFIER;select @NewID = NEWID();select @NewID;";
            return (SqlHelper.ExecuteScalar(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql).ToString());
        }

        // Reads the current status of the task - returns a string with two parts: Status Message~PercentComplete
        public string GetProcessStatus(string GUID)
        {
            //ProcessStatus ret = new ProcessStatus();
            string ret = "";
            string sql = "select ProcessStatusMessage, PercentComplete from ProcessStatus where GUID = @GUID";
            using (SqlDataReader dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
                new SqlParameter("GUID", GUID)))
            {
                if (dr.Read())
                {
                    ret = dr[0].ToString() + "~";
                    if (dr[1] != DBNull.Value)
                    {
                        ret += dr[1].ToString(); //Tack on the percentagecomplete
                    }
                    //ret.ProcessStatusMessage = dr[0].ToString();
                    //if (dr[1] == DBNull.Value)
                    //    ret.PercentComplete = null;
                    //else
                    //    ret.PercentComplete = (Int16?)dr[1];
                    //ret.PercentComplete = (Int16)dr[1];
                }
            }
            return ret;
        }

        public void EndProcess(string ProcessGUID)
        {
            string sql = "update ProcessStatus set Ended = getdate() where GUID = @GUID";
            SqlHelper.ExecuteNonQuery(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
                new SqlParameter("GUID", ProcessGUID));
        }
    }

    public class DataImportExport :StillVoiceCommon.DataTableRow
    {
        public DataImportExport()
        {
            this.initClass(TN5Y.Utilities.DBConnectionString, "DataImportExport");
        }

        internal void loadByGUID(string ProcessGUID)
        {
            Int32 ID;
            ID = Convert.ToInt32( SqlHelper.ExecuteScalar(TN5Y.Utilities.DBConnectionString,CommandType.Text,
                            "select ID from DataImportExport where ProcessGUID = @ProcessGUID",
                            new SqlParameter("ProcessGUID",ProcessGUID)));
            base.load(ID);
        }
    }
}